import { decimalArrayToHexString } from "../utils/utils.js";
import SqLiteLog from "@/js_sdk/sqLite/log.js";
import store from "@/store/index.js";
import dayjs from "dayjs";
import LOG4J from "@/common/utils/Log4j";
const log4j = LOG4J.getLogCat("BleSDK");
export default class BleSDK {
	#queue;
	#isProcessing;

	constructor() {
		this.#queue = [];
		this.#isProcessing = false;
	}

	static send(command) {
		console.log("下发", {
			deviceId: store.state.bleManage.connectedDevice.deviceId, // 设备id
			serviceId: store.state.bleManage.characteristics.service, // 主服务id
			characteristicId: store.state.bleManage.characteristics.write, // 写入id
		});

		// const date = dayjs();
		// SqLiteLog.insertData('BleSDK', "LOG", 'SEND CMD', command, date.format("YYYY-MM-DD HH:mm:ss.SSS"), date.valueOf());	
		log4j.log("SEND CMD", decimalArrayToHexString(command));

		return new Promise((resolve, reject) => {
			const backTimeout = setTimeout(resolve, 3000);
			uni.writeBLECharacteristicValue({
				deviceId: store.state.bleManage.connectedDevice.deviceId, // 设备id
				serviceId: store.state.bleManage.characteristics.service, // 主服务id
				characteristicId: store.state.bleManage.characteristics.write, // 写入id
				value: command,
				success(res) {
					console.log("✅✅✅✅✅✅✅✅✅✅✅✅✅✅✅✅✅✅");
					console.log("BLE-SDK-写入成功", { ...res, cmd: decimalArrayToHexString(command) });
					// LogCat.d("BLE-SDK-写入成功", { ...res, cmd: decimalArrayToHexString(command) });
					// LogCat.r("cmd", decimalArrayToHexString(command));
					console.log("✅✅✅✅✅✅✅✅✅✅✅✅✅✅✅✅✅✅");
					log4j.log("SEND CMD SUCCESS", decimalArrayToHexString(command), res);
					clearTimeout(backTimeout);
					resolve(res);
				},
				fail(err) {
					console.log("❌❌❌❌❌❌❌❌❌❌❌❌❌❌❌❌❌❌");
					console.log("BLE-SDK-写入失败", { ...err, cmd: decimalArrayToHexString(command) });
					// LogCat.d("BLE-SDK-写入失败", { ...err, cmd: decimalArrayToHexString(command) });
					console.log("❌❌❌❌❌❌❌❌❌❌❌❌❌❌❌❌❌❌");
					log4j.error("SEND CMD ERROR", decimalArrayToHexString(command), err);
					clearTimeout(backTimeout);
					reject(err);
				},
			});
		});
	}
}
